package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet2 extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.init(config);//重写init方法里一定要调用父类的init(ServletConfig)操作
        System.out.println("重写了init初始化方法，做了一些工作");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("helloServlet2 的get方法");
        ServletConfig servletConfig = getServletConfig();
        //1，可以获取Servlet 程序的别名servlet -name的值
        System.out.println("HelloServlet程序的别名是: " + servletConfig.getServletName());
        //2，获取初始化参数init - param
        System.out.println("初始化参数username的值是: " + servletConfig.getInitParameter("username2"));
        System.out.println("初始化参数url的值是: " + servletConfig.getInitParameter("url2"));
        //3，获取ServletContext对象
        System.out.println(servletConfig.getServletContext());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("helloServlet2 的post方法");
    }
}
